package com.adcc.swingui.util;

import java.awt.Component;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.adcc.swingui.util.I18n;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
@SuppressWarnings("unchecked")
public class WinMsgBox {
	private static String repMark = "%";

	private WinMsgBox() {
	}
	/**
	 * 显示系统级异常，error串是业务异常抛出，经过国际化
	 * @param parent 父窗体
	 * @param error 错误消息
	 * @return
	 */
	public static void showError(Component parent, String error) {
		ResourceBundle sysMsgRb = I18n.errrb;
		String okString = I18n.getLabel(sysMsgRb, "class WinMsgBox", "OK");
		String[] options = new String[] { okString };
		JOptionPane.showOptionDialog(parent, error, "", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
	}

	/**
	 * 显示对话框
	 * 
	 * @param parent
	 *            父窗口
	 * @param msgCode
	 *            消息编号
	 * @return
	 */
	public static int showDialog(Component parent, int msgCode) {
		return showDialog(parent, null, null, msgCode, null);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param msgCode
	 *            消息编号
	 * @param detail
	 *            动态加值
	 * @return
	 */
	public static int showDialog(Component parent, int msgCode, String[] detail) {
		return showDialog(parent, null, null, msgCode, detail);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param replace
	 *            替换字符串
	 * @param msgCode
	 *            消息编号
	 * @return
	 */
	public static int showDialog(Component parent, String[] replace, int msgCode) {
		return showDialog(parent, null, replace, msgCode, null);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param replace
	 *            替换字符串
	 * @param msgCode
	 *            消息编号
	 * @param detail
	 *            动态加值
	 * @return
	 */
	public static int showDialog(Component parent, String[] replace, int msgCode, String[] detail) {
		return showDialog(parent, null, replace, msgCode, detail);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param addTitle
	 *            对话框标题
	 * @param msgCode
	 *            消息编号
	 * @return
	 */
	public static int showDialog(Component parent, String addTitle, int msgCode) {
		return showDialog(parent, addTitle, null, msgCode, null);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param addTitle
	 *            对话框标题
	 * @param msgCode
	 *            消息编号
	 * @param detail
	 *            动态加值
	 * @return
	 */
	public static int showDialog(Component parent, String addTitle, int msgCode, String[] detail) {
		return showDialog(parent, addTitle, null, msgCode, detail);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param addTitle
	 *            对话框标题
	 * @param replace
	 *            替换字符串
	 * @param msgCode
	 *            消息编号
	 * @return
	 */
	public static int showDialog(Component parent, String addTitle, String[] replace, int msgCode) {
		return showDialog(parent, addTitle, replace, msgCode, null);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param addTitle
	 *            对话框标题
	 * @param replace
	 *            替换字符串
	 * @param msgCode
	 *            消息编号
	 * @param detail
	 *            动态加值
	 * @return
	 */
	public static int showDialog(Component parent, String addTitle, String[] replace, int msgCode, String[] detail) {
		ResourceBundle rb = null;
		rb = I18n.errrb;
		return showDialog(parent, rb, addTitle, replace, msgCode, detail);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param addTitle
	 *            对话框标题
	 * @param resourcePath
	 *            国际化资源路径
	 * @param replace
	 *            替换字符串
	 * @param msgCode
	 *            消息编号
	 * @param detail
	 *            动态加值
	 * @return
	 */
	public static int showDialog(Component parent, String addTitle, String resourcePath, String[] replace, int msgCode, String[] detail) {
		ResourceBundle rb = I18n.errrb;
		return showDialog(parent, rb, addTitle, replace, msgCode, detail);
	}

	/**
	 * 
	 * @param parent
	 *            父窗口
	 * @param rb
	 *            国际化资源
	 * @param addTitle
	 *            对话框标题
	 * @param replace
	 *            替换字符串
	 * @param msgCode
	 *            消息编号
	 * @param detail
	 *            动态加值
	 * @return
	 */
	public static int showDialog(Component parent, ResourceBundle rb, String addTitle, String[] replace, int msgCode, String[] detail) {
		ResourceBundle sysMsgRb = I18n.errrb;
		// Title build
		String msgTitle = "";
		if (addTitle != null) {
			msgTitle += " " + addTitle;
		}
		// Message build
		String cd = Integer.toString(msgCode);
		String mk = null;
		if (cd.length() < 2) {
			mk = "000" + cd;
		} else if (cd.length() < 3) {
			mk = "00" + cd;
		} else if (cd.length() < 4) {
			mk = "0" + cd;
		} else {
			mk = cd;
		}
		Vector val = new Vector();
		for (int i = 0;; i++) {
			try {
				val.add(rb.getString(mk + "." + Integer.toString(i)));
			} catch (MissingResourceException e) {
				break;
			}
		}
		// Message type/icon build
		cd = "P"; // PLAIN_MESSAGE;
		try {
			cd = rb.getString(mk + ".typ");
		} catch (MissingResourceException e) {
		}
		int msgType = JOptionPane.PLAIN_MESSAGE;
		int optType = JOptionPane.DEFAULT_OPTION;
		String yesString = I18n.getLabel(sysMsgRb, "class WinMsgBox", "YES");
		String noString = I18n.getLabel(sysMsgRb, "class WinMsgBox", "NO");
		String cancelString = I18n.getLabel(sysMsgRb, "class WinMsgBox", "CANCEL");
		String okString = I18n.getLabel(sysMsgRb, "class WinMsgBox", "OK");
		String[] options = new String[] { okString };
		switch (cd.toUpperCase().charAt(0)) {
		case 'C': // confirm yes/no
			msgType = JOptionPane.QUESTION_MESSAGE;
			optType = JOptionPane.YES_NO_OPTION;
			options = new String[] { yesString, noString };
			break;
		case 'T': // third confirm yes/no/cancel
			msgType = JOptionPane.QUESTION_MESSAGE;
			optType = JOptionPane.YES_NO_CANCEL_OPTION;
			options = new String[] { yesString, noString, cancelString };
			break;
		case 'I': // information
			msgType = JOptionPane.INFORMATION_MESSAGE;
			break;
		case 'W': // warning
			msgType = JOptionPane.WARNING_MESSAGE;
			break;
		case 'E': // error
			msgType = JOptionPane.ERROR_MESSAGE;
			break;
		}
		// Message replacement build
		if (replace != null) {
			replaceArg(val, replace);
		}
		// Message detail build
		if (detail != null) {
			for (int i = 0; i < detail.length; i++) {
				if (detail[i] != null) {
					val.add(detail[i]);
				}
			}
		}
		String[] msgText = new String[val.size()];
		val.copyInto(msgText);
		val.clear();

		int ret = JOptionPane.showOptionDialog(parent, msgText, msgTitle, optType, msgType, null, options, options[0]);

		return ret;
	}

	/**
	 * 参数替换
	 * 
	 * @param str
	 * @param arg
	 */
	private static void replaceArg(Vector str, String[] arg) {
		int line = str.size();
		for (int i = 0; i < line; i++) {
			String m = replaceArg(str.get(i).toString(), arg);
			str.set(i, m);
		}
	}

	/**
	 * 单个参数替换
	 * 
	 * @param str
	 * @param arg
	 * @return
	 */
	private static String replaceArg(String str, String[] arg) {
		if (str.indexOf(repMark) >= 0) {
			int argc = arg.length;
			for (int j = 0; j < argc; j++) {
				if (arg[j] != null) {
					str = str.replaceFirst(repMark + Integer.toString(j), arg[j]);
				}
				if (str.indexOf(repMark) < 0) {
					break;
				}
			}
		}
		return str;
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(new WindowsLookAndFeel());
		} catch (UnsupportedLookAndFeelException e) {
		}
		JFrame jFrame = new JFrame("hello");
		jFrame.setSize(800, 600);
		jFrame.setVisible(true);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		WinMsgBox.showError(jFrame, "程序出错");
		WinMsgBox.showDialog(jFrame, 1000);
		WinMsgBox.showDialog(jFrame, "你好", new String[] { "真" }, 1001);
		WinMsgBox.showDialog(jFrame, "你好", 1004);
		WinMsgBox.showDialog(jFrame, "你好", new String[] { "真" }, 1005);
		WinMsgBox.showDialog(jFrame, "你好", new String[] { "真", "真" }, 1009);
	}
}